package dao.impl;

import dao.UserDao;
import po.User;
import util.DataSourceUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao {
    @Override
    public int getNumOfPhone(String Phone) throws Exception {

        Connection con = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs =null;
        int count = -1;

        // 获取数据库链接
        con = DataSourceUtil.getConnection();
        // 准备sql
        String sql ="select count(*) from t_user where phone=?";
        preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1,Phone);


        // 发送执行
       rs= preparedStatement.executeQuery();

        // 处理结果
        rs.next();

        count= rs.getInt(1);
        // 归还connection对象

        DataSourceUtil.release(con,preparedStatement,rs);


        return count;
    }

    @Override
    public boolean addUser(User user) throws Exception {
        String sql ="insert into t_user(id,phone,pwd)values(?,?,?)";
        Connection con= null;
        PreparedStatement pstmt= null;
        con=DataSourceUtil.getConnection();
        pstmt = con.prepareStatement(sql);
        pstmt.setString(1,user.getId());
        pstmt.setString(2,user.getPhone());
        pstmt.setString(3,user.getPwd());

        int count =pstmt.executeUpdate();

        DataSourceUtil.release(con,pstmt,null);



        return count == 0 ? false:true;
    }
}
